package com.haveajob.bill.service;

import com.haveajob.bill.dao.BillMapper;
import com.haveajob.bill.entity.Bill;
import com.haveajob.employee.dao.EmployeeMapper;
import com.haveajob.employee.entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("billServiceImpl")
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public Bill selectBillDeposit(Bill bill) {
        return billMapper.selectBillDeposit(bill);
    }

    @Override
    public int countTheDayOrder(String date) {
        return billMapper.countTheDayOrder(date);
    }

    @Transactional
    @Override
    public int insertSelectiveBill(Bill record,int userId) {

        //创建工人对象，修改工人信息
        Employee employee = new Employee();
        employee.setSecuritydeposituser(true);
        employee.setPaysecuritydeposittime(record.getTransactiontime());
        employee.setUserid(userId);

        //修改工人
        int i = employeeMapper.updateByPrimaryKeySelective(employee);
        //插入账单
        int j = billMapper.insertSelectiveBill(record);

        return i>0&&j>0?1:0;
    }

    @Transactional
    @Override
    public int backMoneyInsertBill(Bill record, int userId) {
        //创建工人对象，修改工人信息
        Employee employee = new Employee();
        employee.setSecuritydeposituser(false);
        employee.setPaysecuritydeposittime(null);
        employee.setUserid(userId);

        //修改工人
        int i = employeeMapper.updateByPrimaryKeySelective(employee);
        //插入账单
        int j = billMapper.insertSelectiveBill(record);

        return i>0&&j>0?1:0;
    }

    @Override
    public int addBill(Bill bill) {
        return billMapper.addBill(bill);
    }

    @Override
    public List<Bill> selectUserBills(int userId, String startDate, String endDate) {
        return billMapper.selectUserBills(userId,startDate,endDate);
    }

    @Override
    public int insertBill(Bill record) {
        return billMapper.insertSelectiveBill(record);
    }

    @Override
    public List<Bill> selectEmployeeOrderBills(int userId, String startDate, String endDate) {
        return billMapper.selectEmployeeOrderBills(userId,startDate,endDate);
    }


}
